#include "Plane.h"

Plane::Plane()
{
	normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	distance = 0.0f;
	colour = Colour(1.0f, 0.0f, 0.0f, 1.0f);
}

Plane::Plane(const D3DXVECTOR3& norm, float dist, const Colour& col)
{
	normal = norm;
	distance = dist;
	colour = col;
}

const D3DXVECTOR3& Plane::GetNormalAt(const D3DXVECTOR3& point)
{
	return normal;
}

float Plane::FindIntersection(const Ray& ray)
{
	D3DXVECTOR3 rayDir = ray.GetDirection();
	
	float dotProd = D3DXVec3Dot(&rayDir, &normal);

	if (dotProd == 0)
	{
		// ray = parrallel to plane
		return -1.0f;
	}

	D3DXVECTOR3 toAdd = -(normal * distance);
	D3DXVECTOR3 rhsDot(ray.GetOrigin() + toAdd);
	//float dotNormal = D3DXVec3Dot(&normal, &D3DXVECTOR3(ray.GetOrigin() + (normal * -distance)));
	float dotNormal = D3DXVec3Dot(&normal, &rhsDot);
	float result = -1.0f * (dotNormal / dotProd);
	return result;
}